// 判断是否为数组
function isArray (o) {
  return Object.prototype.toString.call(o) === '[object Array]'
}

var datetime = {
  dbDate: function (date) {
    date = date.toString()
    date = date.length === 1 ? '0' + date : date
    return date
  },
  getMonthDay: function (yms) {
    // yms 是日期数组
    if (!isArray(yms)) {
      console.warn('\'yms\' must be a Array')
      return false
    } else {
      var allmonths = []
      var today = new Date()
      for (var i = 0; i < yms.length; i++) {
        var monthdays = []
        var datenum = getMonthDateNumber(yms[i])
        for (var j = 0; j < datenum; j++) {
          var temdate = new Date(yms[i].getFullYear(), yms[i].getMonth(), j + 1)
          var holidayshow = false
          var holidayname = ''
          var able
          if (temdate < new Date(today.getFullYear(), today.getMonth(), today.getDate())) {
            able = false
          } else {
            able = true
          }
          var ymd = temdate.getFullYear() + '-' + datetime.dbDate(temdate.getMonth() + 1) + '-' + datetime.dbDate(temdate.getDate())
          monthdays[ymd] = {
            year: temdate.getFullYear(),
            month: datetime.dbDate(temdate.getMonth() + 1),
            date: datetime.dbDate(temdate.getDate()),
            ymd: ymd,
            week: temdate.getDay(),
            isholiday: holidayshow,
            holidayname: holidayname,
            able: able,
            ispick: false
          }
        }
        allmonths.push(monthdays)
      }
      return allmonths
    }

    // 获取一个月的天数
    function getMonthDateNumber (date) {
      var temdate = new Date(date.getFullYear(), date.getMonth() + 1, 0)
      return temdate.getDate()
    }
  }
}

export default datetime
